package com.hmyd.hmyd_android.ui;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.FindSecurity;
import com.hmyd.hmyd_android.utils.MyToast;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * 修改安全问题
 * 
 * @author wangc
 * 
 */
@ContentView(R.layout.activity_change_security_issues)
public class ChangeSecurityIssuesActivity extends BasicActivity {
	// 标题栏
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	@ViewInject(R.id.tv_securityquestion_question_one)
	private TextView mTvQuestionOne;
	@ViewInject(R.id.tv_securityquestion_question_two)
	private TextView mTvQuestionTwo;

	@ViewInject(R.id.ed_securityquestion_answer_one)
	private EditText mEdAnswerOne;
	@ViewInject(R.id.ed_securityquestion_answer_two)
	private EditText mEdAnswerTwo;

	@ViewInject(R.id.submit)
	private Button mButton;
	private String code1 = null;
	private String code2 = null;

	private final int FINDSECURITYQUESTION = 1;
	private final int VALIDSECURITYQUESTION = 2;

	@OnClick({ R.id.submit, R.id.forget })
	private void onClick(View view) {
		switch (view.getId()) {
		case R.id.submit:
			next();
			break;
		case R.id.forget:
			startActivity(new Intent(ChangeSecurityIssuesActivity.this, ResetQuestionActivity.class));
			break;

		default:
			break;
		}
	}

	private void next() {

		String answer1 = mEdAnswerOne.getText().toString().trim();
		String answer2 = mEdAnswerTwo.getText().toString().trim();

		if (TextUtils.isEmpty(answer1)) {
			MyToast.showHMToast(this, false, "请回答第一个问题");
			return;
		}

		if (TextUtils.isEmpty(answer2)) {
			MyToast.showHMToast(this, false, "请回答第二个问题");
			return;
		}

		RequestParams params = new RequestParams();
		params.addBodyParameter("code1", code1);
		params.addBodyParameter("code2", code2);
		params.addBodyParameter("answer1", answer1);
		params.addBodyParameter("answer2", answer2);

		onRequest(VALIDSECURITYQUESTION, UrlConstant.VALIDSECURITYQUESTION, HttpMethod.POST, params);

	}

	@Override
	protected void onResume() {
		super.onResume();
		onRequest(FINDSECURITYQUESTION, UrlConstant.FINDSECURITYQUESTION, HttpMethod.GET, null);
	}

	@Override
	public void onPostSuccess(int postId, String result) {

		Gson gson = new Gson();
		switch (postId) {
		case FINDSECURITYQUESTION:
			java.lang.reflect.Type type = new TypeToken<FindSecurity>() {
			}.getType();
			FindSecurity findSecurity = gson.fromJson(result, type);

			String[] questions_en = getResources().getStringArray(R.array.question_en);
			String[] question = getResources().getStringArray(R.array.question);

			String question1 = null;
			String question2 = null;
			code1 = findSecurity.code1;
			code2 = findSecurity.code2;
			for (int i = 0; i < questions_en.length; i++) {
				if (findSecurity.code1.equals(questions_en[i])) {
					question1 = question[i];
				}

				if (findSecurity.code2.equals(questions_en[i])) {
					question2 = question[i];
				}
			}

			mTvQuestionOne.setText(question1);
			mTvQuestionTwo.setText(question2);
			break;
		case VALIDSECURITYQUESTION:
			startActivity(new Intent(ChangeSecurityIssuesActivity.this,
					SecurityIssuesActivity.class));
			break;
		default:
			break;
		}

	}

	@Override
	public void onPostFailure(int postId, String msg) {

	}

	@Override
	protected void setParams() {
		home_title_txt.setText("修改安全问题");
		home_title.setOnTouchListener(touch);
		mEdAnswerOne.addTextChangedListener(watcher);
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}

	private TextWatcher watcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {

		}

		@Override
		public void afterTextChanged(Editable s) {
			if (!TextUtils.isEmpty(s.toString())) {
				mButton.setEnabled(true);
			}
		}
	};

}
